#pragma once
#include <iostream>;
#include <string>
#include <vector>
#include <sys/mman.h>
using std::cout;
using std::endl;

void* FetchMemoryFromSystem(size_t bit_num)
{
    void *ptr = mmap(nullptr, bit_num, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (ptr == MAP_FAILED) {
        perror("mmap");
        return nullptr;
    }
    return ptr;
}

template<class T>
class memory_pool
{
public:
	T* New()
	{
		if (_list)
		{
			
		}
		T* obj = nullptr;
		if (_remain < sizeof(T))
		{
			_remain = 128 * 1024;
			_memory = (char*)FetchMemoryFromSystem(_remain);
		}
		obj = (T*)_memory;
		int size = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);
		_memory += size;
		_remain -= size;

		new(obj)T;
		return obj;
	}

	void Delete(T* obj)
	{
		obj->~T();
		*(void**)obj = _list;
		_list = obj;
	}
private:
	char* _memory = nullptr;
	void* _list = nullptr;
	int _remain = 0;
};